package com.kedacom.emptyproject.map.activity


import android.databinding.ViewDataBinding
import android.os.Bundle
import android.widget.Toast
import com.kedacom.lego.fast.view.LegoFastActivity
import com.kedacom.lego.fast.widget.LegoToast
import com.kedacom.lego.mvvm.viewmodel.LegoBaseViewModel
import com.kedacom.widget.common.StatusBarUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlin.coroutines.CoroutineContext


/** Created by wangwei on 2020/9/18 14:27 */
open class BaseActivity<V : ViewDataBinding, VM : LegoBaseViewModel> : LegoFastActivity<V, VM>(),
    CoroutineScope {
    protected lateinit var job: Job
    lateinit var mBinding: V
    lateinit var mViewModel: VM

    override val coroutineContext: CoroutineContext
        get() = job + Dispatchers.Main

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = viewDataBinding
        mViewModel = viewModel
        job = Job()
        StatusBarUtil.statusBarImmersion(this)//设置沉浸式状态栏
        StatusBarUtil.statusBarMode(this, true)//设置深色字体
    }

    override fun onDestroy() {
        super.onDestroy()
        job.cancel()
    }

    fun showToast(msg: String?, error: Boolean = false) {
        msg?.let {
            if (error) {
                LegoToast.error(this, msg, Toast.LENGTH_SHORT)
            } else {
                LegoToast.info(this, msg, Toast.LENGTH_SHORT)
            }.show()
        }
    }

}